package com.simplegovern.setting;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

public class FormHtmlBulider {

	private Setting setting;
	private String FORM_TEXT = "text";
	private String FORM_RADIO = "radio";
	private String FORM_TEXTAREA = "textarea";
	private String FORM_SELECT = "select";
	
	public FormHtmlBulider(Setting setting){
		this.setting = setting;
	}
	
	public String getOption(){
		String type = setting.getType();
		//构造文本框
		if(FORM_TEXT.equals(type))
			return text();
		//构造单选框
		else if(FORM_RADIO.equals(type))
			return radio();
		//构造文本域
		else if(FORM_TEXTAREA.equals(type))
			return textarea();
		//构造下拉框
		else if(FORM_SELECT.equals(type))
			return select();
		return unknown();
	}
	
	private String text(){
		StringBuffer html = new StringBuffer();
		html.append("<input type=\"text\" ");
		html.append(getName());
		html.append(getValue());
		html.append(getFormClass());
		html.append("/>");
		return html.toString();
	}
	
	private String textarea(){
		StringBuffer html = new StringBuffer();
		html.append("<textarea ");
		html.append(getName());
		html.append(getFormClass());
		html.append("/>");
		html.append(setting.getValue());
		html.append("</textarea>");
		return html.toString();
	}
	
	private String radio(){
		StringBuffer html = new StringBuffer();
		Map<String, String> map = getItemsMap();
		html.append("<br/>");
		for (String key : map.keySet()) {
			String value = map.get(key);
			html.append("<input type=\"radio\" ");
			html.append(getName());
			html.append(getFormClass());
			html.append("value=\""+value+"\" ");
			//选择
			if(StringUtils.equals(setting.getValue(), value)){
				html.append("checked=\"checked\" ");
			}
			html.append("/> ");
			html.append(key);
			html.append("<br/>");
		}
		
		return html.toString();
	}
	
	private String select(){
		StringBuffer html = new StringBuffer();
		Map<String, String> map = getItemsMap();
		html.append("<br/>");
		html.append("<select ");
		html.append(getName());
		html.append(getFormClass());
		html.append(">");
		for (String key : map.keySet()) {
			String value = map.get(key);
			html.append("<option ");
			html.append("value=\""+value+"\" ");
			//选择
			if(StringUtils.equals(setting.getValue(), value)){
				html.append("selected=\"selected\" ");
			}
			html.append(" />");
			html.append(key);
			html.append("</option>");
		}

		html.append("</select>");
		return html.toString();
	}
	
	private Map<String, String> getItemsMap(){
		String items = setting.getItems();
		Map<String, String> map = new HashMap<String, String>();
		if(items!=null){
			JSONObject jsonObject = JSONObject.fromObject(items);
			map = (Map<String, String>)JSONObject.toBean(jsonObject, Map.class);
		}
		return map;
	}
	
	private String getName(){
		return "name=\"setting.value\" ";
	}
	
	private String getValue(){
		return "value=\""+setting.getValue()+"\" ";
	}
	
	private String getFormClass(){
		return "class=\""+setting.getRule()+"\" ";
	}
	
	private String unknown(){
		return "程序出错，未知的表单";
	}
}
